package com.atjinggu.jinggumail.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jingming
 * @Type CustomFeignConfig.java
 * @Desc
 * @date 2021/3/8 10:39
 */
@Configuration
public class CustomFeignConfig {

    @Bean
    public RequestInterceptor logFeignRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                //老请求
                if(null !=  attributes) {
                    HttpServletRequest request = attributes.getRequest();
                    String cookie = request.getHeader("Cookie");
                    //同步cookie从老请求头到feign请求头
                    requestTemplate.header("Cookie", cookie);
                }
            }
        };
    }
}